<template>
  <div class="container" @scroll="remberPos" ref="scrollBox">
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="detail">
      <h3 class="title">{{ articleDetail.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="articleDetail.aut_photo"
        />
        <div class="text">
          <p class="name">{{ articleDetail.aut_name }}</p>
          <p class="time">{{ articleDetail.pubdate | relaTime }}</p>
        </div>
        <van-button round size="small" type="info" @click="toggleFollw">{{
          articleDetail.is_followed ? "已经关注" : "+ 点击关注作者"
        }}</van-button>
      </div>
      <div class="content" v-html="articleDetail.content"></div>
      <div class="zan">
        <van-button
          round
          size="small"
          :class="{ active: articleDetail.attitude == 1 }"
          plain
          icon="like-o"
          @click="toggleState('like')"
          >点赞</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          :class="{ active: articleDetail.attitude == 0 }"
          plain
          icon="delete"
          @click="toggleState('dislike')"
          >不喜欢</van-button
        >
      </div>
      <Comment></Comment>
    </div>
  </div>
</template>

<script>
import Comment from '@/views/article/components/comment.vue'
import { unfollowApi, followApi } from '@/api/user.js'
import { getDetailByIdApi, disLike, unDisLike, like, unLike } from '@/api/articleDetailAPI.js'
export default {
  name: 'article-index',
  components: {
    Comment
  },
  data () {
    return {
      articleDetail: {},
      scrollHight: 0
    }
  },
  methods: {
    async getDetailById () {
      const { data } = await getDetailByIdApi(this.$route.params.id)
      // console.log('文章详情', data)
      this.articleDetail = data
    },
    remberPos (e) {
      this.scrollHight = e.target.scrollTop
    },
    async toggleFollw () {
      try {
        if (this.articleDetail.is_followed) {
          // 取关操作
          // 1.后台数据取关
          await unfollowApi(this.articleDetail.aut_id)
          // 2.前端界面取关
          this.articleDetail.is_followed = false
        } else {
          // 关注操作
          await followApi(this.articleDetail.aut_id)
          this.articleDetail.is_followed = true
        }
      } catch (error) {
        console.dir(error)
        this.$toast.fail('操作失败')
      }
    },
    // 用户切换对文章的态度
    async toggleState (str) {
      // 用传过来的形参str代表用户点击了哪个按钮: 'like'喜欢按钮    'dislike'不喜欢按钮
      try {
        if (str === 'like') {
          // 喜欢按钮
          if (this.articleDetail.attitude === 1) {
            // 取消喜欢
            await unLike(this.articleDetail.art_id)
            // 把状态变为-1 既不喜欢 也 没有不喜欢
            this.articleDetail.attitude = -1
          } else {
            // 喜欢文章
            await like(this.articleDetail.art_id)
            this.articleDetail.attitude = 1
          }
        } else {
          // 不喜欢按钮
          if (this.articleDetail.attitude === 0) {
            // 取消不喜欢
            await unDisLike(this.articleDetail.art_id)
            this.articleDetail.attitude = -1
          } else {
            // 不喜欢
            await disLike(this.articleDetail.art_id)
            this.articleDetail.attitude = 0
          }
        }
        this.$toast.success('操作成功')
      } catch (error) {
        this.$toast.fail('操作失败')
      }
    }
  },
  activated () {
    // 坚强服务器压力,和性能优化 如果用户点击的文章id和上次一样就没必要再从发送一次请求渲染了
    if (this.articleDetail.art_id !== this.$route.params.id) {
      this.getDetailById()
      // 文章不一样就把滚动高度变为0 不需要再执行 阅读记忆
      this.scrollHight = 0
    } else {
      // 文章一样===>上次点击然后用户又点回来了===>实现阅读记忆
      // this.$refs.scrollBox.scrollTop = this.scrollHight
      // 再一步性能优化===>判断用户滚每滚 如果没滚 ===>下次进入也就不用记忆阅读了===>提高渲染性能
      this.scrollHight && (this.$refs.scrollBox.scrollTop = this.scrollHight)
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.detail {
  padding: 46px 10px 44px;
  // height: 1000%;
  .title {
    font-size: 18px;
    line-height: 2;
  }
  .zan {
    text-align: center;
    padding: 10px 0;
    .active {
      border-color: red;
      color: red;
    }
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.5;
      text-align: left;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  // 内容中包含：img 特别宽  code pre 不能换行
  .content {
    padding: 20px 0;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
    /deep/ code {
      white-space: pre-wrap;
    }
    /deep/ pre {
      white-space: pre-wrap;
    }
  }
}
</style>
